package com.zhuss.meeting.processor;

import com.zhuss.bean.bo.meeting.apply.ChangeApplyBo;
import com.zhuss.bean.vo.meeting.apply.DetailApplyVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MeetRoomCheck;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.meeting.db.dao.ext.MeetingApplyDao;
import com.zhuss.meeting.db.dao.ext.MeetingRoomTimeDao;
import com.zhuss.meeting.db.entity.MeetingRoomApply;
import com.zhuss.meeting.db.entity.MeetingRoomTime;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/16 18:52
 */
@Component("applyChange")
public class ApplyChange extends AbstractProcessor<Object, ChangeApplyBo> implements Processor<Object, ChangeApplyBo> {

    @Autowired
    private MeetingRoomTimeDao meetingRoomTimeDao;

    @Autowired
    private MeetingApplyDao meetingApplyDao;

    @Autowired
    private ApplyRead applyRead;


    @Override
    public Object process(ChangeApplyBo bo) throws WpsException {
        DetailApplyVo detail = (DetailApplyVo)applyRead.detail(bo.getApplyId());
        MeetingRoomTime m = new MeetingRoomTime();

        m.setRoomId(detail.getRoomId());
        m.setMeetingStartTime(bo.getMeetingStartTime());
        m.setMeetingEndTime(bo.getMeetingEndTime());

        m.setModifier(bo.getConcurrentUserName());
        m.setModifierId(bo.getConcurrentUserId());
        m.setModifyTime(new Date());
        m.setDeleted(301);
        meetingRoomTimeDao.delete(m);

        MeetingRoomTime t = new MeetingRoomTime();
        t.setApplyId(bo.getApplyId());
        t.setRoomId(bo.getRoomId());
        t.setRoomName(bo.getRoomName());
        t.setMeetingStartTime(bo.getMeetingStartTime());
        t.setMeetingEndTime(bo.getMeetingEndTime());
        t.setArea(bo.getArea());

        t.setCreator(bo.getConcurrentUserName());
        t.setCreatorId(bo.getConcurrentUserId());
        t.setCreateTime(new Date());
        meetingRoomTimeDao.create(t);

        MeetingRoomApply apply = meetingApplyDao.applyDetail(bo.getApplyId());
        apply.setRoomId(t.getRoomId());
        apply.setRoomName(t.getRoomName());
        apply.setStatus(MeetRoomCheck.更改会议室.key());
        meetingApplyDao.updateByPrimaryKeySelective(apply);

        return null;
    }

    @Override
    public Object log(ChangeApplyBo param) throws WpsException {
        DetailApplyVo detail = (DetailApplyVo)applyRead.detail(param.getApplyId());
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.会务.key());
        record.setAction(LogActionEnum.更改会议室.key());
        record.setTargetId(param.getApplyId());
        record.setTargetName(detail.getRoomName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }

    @Override
    public void check(ChangeApplyBo changeApplyBo) throws WpsException {

    }
}
